/*------------------------------------------------------------------------------
 * Copyright 2012 NCR Corporation
 *------------------------------------------------------------------------------
 * All revision information is updated automatically from source code control
 * change records - please do not manually edit.
 *------------------------------------------------------------------------------
 *
 *  $LastChangedRevision::                                          $
 *  $LastChangedBy::                                                $
 *  $LastChangedDate::                                              $
 */

package com.ncr.nep.dashboardcore.mvp.client;

import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Widget;

/**
 * {@link WidgetView} that integrates with
 * {@link com.google.gwt.uibinder.client.UiBinder} to display a user interface
 * based on a {@link Widget}.
 *
 * @author rb250096
 * @version $Revision$
 */
@SuppressWarnings("rawtypes")
public abstract class WidgetViewBinder<T extends Presenter>
        implements WidgetView {

    //--------------------------------------------------------------------------
    //
    //  Fields
    //
    //--------------------------------------------------------------------------
    
    /**
     * The {@link Presenter} associated with this view.  All UI interaction
     * logic involving the model is delegated to this object.
     */
    protected T presenter;

    /**
     * Widget generated by a {@link com.google.gwt.uibinder.client.UiBinder}
     * instance that is displayed in this view.
     */
    protected Widget root;

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    @Override
    public Presenter<?> getPresenter() {
        return this.presenter;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void setPresenter(Presenter<?> presenter) {
        this.presenter = (T)presenter;
    }

    @Override
    public boolean isAttached() {
        return this.root.isAttached();
    }

    /**
     * Sets the root {@link Widget} that comprises this view.
     *
     * @param root The root widget.
     */
    public void setRoot(Widget root) {
        this.root = root;
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    @Override
    public HandlerRegistration addAttachHandler(AttachEvent.Handler handler) {
        return this.root.addAttachHandler(handler);
    }

    @Override
    public void fireEvent(GwtEvent<?> gwtEvent) {
        this.root.fireEvent(gwtEvent);
    }

    @Override
    public Widget asWidget() {
        return this.root;
    }

    @Override
    public void init() {}

    @Override
    public void dispose() {}
}
